package io.github.hidroh.materialistic;

import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseActivity extends TrackableActivity {

    protected ViewGroup mContentView;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private View mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        mContentView = (ViewGroup) findViewById(R.id.content_frame);
        mDrawer = findViewById(R.id.drawer);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer,
                R.string.close_drawer);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    //系统使用ActionBarDrawerToggle要同步状态
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isSearchable()) {
            // close search view
            //重画menu
            supportInvalidateOptionsMenu();
        }
    }

    //当设备的的配置信息发生变化的时候，系统会调用这个函数
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /*
    * 当activity启动之后，actionitem被初始化，系统会主动调用这个方法
    * */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (isSearchable()) {
            getMenuInflater().inflate(R.menu.menu_search, menu);
            MenuItem menuSearch = menu.findItem(R.id.menu_search);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuSearch);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(
                    new ComponentName(this, SearchActivity.class)));
            searchView.setIconified(true);
            searchView.setQuery("", false);
        }
        return super.onCreateOptionsMenu(menu);
    }

    //左侧边栏drawer打开或关闭，是否更新menu
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (isSearchable()) {
            return true;
        }

        return super.onPrepareOptionsMenu(menu);
    }

    /*
    *处理点击action button的一些操作，判断点击事件是否来自于app图标，然后决定是关闭还是打开侧边栏drawer
    * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /*
    * 重写ActionBarActivity的setContentView，从一个布局文件设置一个activity的内容，公共的方法，供子类调用
    * reference：https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#setContentView(android.view.View)
    * */
    @Override
    public void setContentView(int layoutResID) {
        mContentView.addView(getLayoutInflater().inflate(layoutResID, mContentView, false));
    }


    /*
    * 重写activity默认关闭当前activity的方法，如果DrawerLayout的左侧边栏，开着的话就关闭
    * */
    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(mDrawer)) {
            closeDrawers();
        } else {
            super.onBackPressed();
        }
    }

    protected void closeDrawers() {
        mDrawerLayout.closeDrawers();
    }

    /**开启一个fragment的事务
     * Begins fragment transaction with no custom animation
     * @return fragment transaction
     */
    protected FragmentTransaction beginFragmentTransaction() {
        return getSupportFragmentManager().beginTransaction();
    }

    /**
     * Removes fragment with given tag if any
     * @param transaction   fragment transaction
     * @param tag           tag of fragment to be removed
     * @return  fragment transaction
     * 每个fragment在加进来的时候，被唯一的一个tag进行标记，通过传递tag来删除特定的fragment，以免fragment遮盖无法显示。
     */
    protected FragmentTransaction removeFragment(FragmentTransaction transaction, String tag) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment != null) {
            transaction.remove(fragment);
        }
        return transaction;
    }

    /**
     * Checks if activity should have search view
     * @return true if is searchable, false otherwise
     *
     * 抽象的方法，让子类实现
     */
    protected abstract boolean isSearchable();
}
